package com.jf.controller;

import android.app.Activity;

import com.jf.assistant.MyAccessibilityService;
import com.jf.bean.Action;
import com.jf.bean.AssistantTask;
import com.jf.utils.Sputils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class MainController {


    public static String ASSISTANT_TASKS="AssistantTasks";

    public static ArrayList<AssistantTask> getAssistantTask(Activity mactivity) throws ParseException {

        ArrayList<AssistantTask> assistantTasks=   Sputils.getObject(MainController.ASSISTANT_TASKS,mactivity);

        return assistantTasks;
    }


    public static ArrayList<AssistantTask> setSampleAssistantTask() throws ParseException {


        AssistantTask assistantTask1 = new AssistantTask();
        SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
        Date d1=ft.parse("1:30:00");
        Calendar calendar1 = Calendar.getInstance();
        calendar1.set(1990,12,1,13,30);
        assistantTask1.setStarttime(calendar1);
        assistantTask1.setTaskname("京东签到测试");

        Action actionname1 = new Action();
        actionname1.setActionname("京东签到");
        actionname1.setType(Action.ACTIONNAME);

        Action actionname2 = new Action();
        Calendar actioncalendar1 = Calendar.getInstance();
        actioncalendar1.set(1990,12,1,13,30);
        actionname2.setStarttime(actioncalendar1);
        actionname2.setType(Action.START_TIME);

        Action actionname3 = new Action();
        actionname3.setOpenappname("京东");
        actionname3.setOpenapppackage("com.pack");
        actionname3.setType(Action.OPENAPP);

        Action actionname4 = new Action();
        actionname4.setDelaytime(1000);
        actionname4.setType(Action.DELAY);

        Action actionname5 = new Action();
        actionname5.setClicktext("领京豆");
        actionname5.setType(Action.CLICK);

        Action actionname6 = new Action();
        actionname6.setDelaytime(1000);
        actionname6.setType(Action.DELAY);

        Action actionname7 = new Action();
        actionname7.setClicktext("领取京豆");
        actionname7.setType(Action.CLICK);



        ArrayList<Action> actions = new ArrayList<>();
        actions.add(actionname1);
        actions.add(actionname2);
        actions.add(actionname3);
        actions.add(actionname4);
        actions.add(actionname5);
        actions.add(actionname6);
        actions.add(actionname7);
        assistantTask1.setActions(actions);


        AssistantTask assistantTask2 = new AssistantTask();
        SimpleDateFormat ft2 = new SimpleDateFormat("HH:mm:ss");
        Date d2=ft2.parse("18:30:00");

        Calendar calendar2 = Calendar.getInstance();
        calendar2.set(1990,12,1,18,30);
        assistantTask2.setStarttime(calendar2);
        assistantTask2.setTaskname("支付宝签到测试");

        ArrayList<AssistantTask> assistantTasks = new ArrayList<>();
        assistantTasks.add(assistantTask1);
        assistantTasks.add(assistantTask2);
        return assistantTasks;
    }


    public static void saveAssistantTask(AssistantTask assistantTask, Activity mactivity) throws ParseException {

        ArrayList<AssistantTask> assistantTask1 = getAssistantTask(mactivity);
        if (assistantTask1!=null){
            assistantTask1.add(assistantTask);
        }else {
            assistantTask1=new ArrayList<AssistantTask>();
            assistantTask1.add(assistantTask);
        }
        Sputils.putObject(ASSISTANT_TASKS,assistantTask1,mactivity);

    }

    public static void deleAssistantTask(int position, Activity mactivity) throws ParseException {

        ArrayList<AssistantTask> assistantTask1 = getAssistantTask(mactivity);
        if (assistantTask1!=null){
            assistantTask1.remove(position);
        }else {

        }
        Sputils.putObject(ASSISTANT_TASKS,assistantTask1,mactivity);

    }

    public static void saveAssistantTask(AssistantTask assistantTask, Activity mactivity,int position) throws ParseException {

        ArrayList<AssistantTask> assistantTask1 = getAssistantTask(mactivity);
        assistantTask1.set(position,assistantTask);
        Sputils.putObject(ASSISTANT_TASKS,assistantTask1,mactivity);

    }


    public static boolean checkIfContainTask(AssistantTask assistantTask, Activity mactivity) throws ParseException {
        ArrayList<AssistantTask> assistantTask1 = getAssistantTask(mactivity);

        if (assistantTask1!=null){
           for (AssistantTask mAssistantTask:assistantTask1){
               if (mAssistantTask.getTaskname().equals(assistantTask.getTaskname())){
                   return true;
               }
           }
        }
        return false;
    }
}
